export default {
    data() {
        return {
            classyShow: false, // 是否弹出分类选择
            classyIndex: 0, // 当前分类索引
            contentList: [],
            contentListCopy: [],
            pages: 1, // 当前页码
            pageSize: 20, // 每页条目数量
            noMore: false, // ，没有更多数据了
            timer: null, // 轮询定时请求
            deviceType: ''
        }
    },
    onLoad() {
        this.getDeviceInfo()
        console.log("这是哈");
        console.log("id", this.$store.state.activityRecordId);

    },
    /*监听底部上拉事件*/
    onReachBottom() {
        if (this.noMore == false) {
            this.pages++;
            this.getPageData();
        }
    },
    methods: {
        // 获取设备信息
        getDeviceInfo() {
            let data = {
                deviceId: this.$store.state.deviceId,
                terminal: "consumer"
            }
            this.$util.request(this.$api.GetDeviceInfo, data, "GET").then(res => {
                this.deviceType = res.result.deviceType
                if (this.deviceType === 13) {
                    // 轮询 
                    this.getPolling()

                } else {
                    // 视觉柜货单
                    this.getPageData();
                }
            })
        },
        // 轮询接口
        getPolling() {
            uni.showLoading({
                title: '加载中'
            });
            console.log("轮询中...");
            let data = {
                activityRecordId: this.$store.state.activityRecordId,
                terminal: "consumer"
            }
            this.$util.request(this.$api.GetPolling, data, "GET", false).then(res => {
                if (res.result === 10) {
                    // 弹出上货单
                    this.getPageData()
                    uni.hideLoading()
                } else {
                    // console.log("非10");
                    //  继续轮询
                    setTimeout(() => {
                        this.getPolling()
                    }, 2000);
                }
            })
        },
        // 获取商品信息
        getPageData: function() {
            console.log("id", this.$store.state.activityRecordId);
            let data = {}
            // if (this.deviceType === 13) {
            //     // 重力柜
            //     data = {
            //         deviceId: this.$store.state.deviceId,
            //         pageNo: this.pages,
            //         pageSize: this.pageSize,
            //         activityId: this.$store.state.activityRecordId,
            //         type: "identify"
            //     }
            // } else {
                // 视觉柜
                data = {
                    deviceId: this.$store.state.deviceId,
                    pageNo: this.pages,
                    pageSize: this.pageSize,
                }
            // }
            let url = "";
            if (this.$store.state.deviceId) { // 有设备id时
                data;
                url = this.$api.GetGoodsListByDevice; // 那个接口
            } else { // 无设备id时
                uni.showModal({
                    title: '提示',
                    content: '请扫描设备上的二维码，以便知道您身边的设备',
                    showCancel: true,
                    cancelText: '取消',
                    confirmText: '扫码',
                    success: res => {
                        if (!res.confirm) {
                            uni.navigateBack();
                            return;
                        }
                        // 扫码API
                        uni.scanCode({
                            onlyFromCamera: false,
                            success: (res) => {
                                this.$util.extractUri(this, res.result)
                                this.getPageData();
                            },
                            complete: (res) => {
                                console.log(res)
                            }
                        });
                    }
                });
                return false;
            }
            this.$util.request(url, data, "GET").then(res => {
                console.log("重力柜", res);
                if (res.code == 200) {
                    res.data = res.result.records || [];
                    let contentList = this.contentList;
                    let hasType = [];
                    if (res.data.length) {
                        res.data.forEach((item, i) => {
                            item.count = item.residueNum;
                            item.saleMode = item.saleMode //售卖方式
                            item.newCount = item.residueNum
                            let index = hasType.indexOf(item.typeName);
                            if (index != -1) {
                                contentList[index].list.push(item);
                            } else {
                                hasType.push(item.typeName);
                                contentList.push({
                                    typeName: item.typeName,
                                    type: item.type,
                                    list: []
                                })
                                contentList[hasType.indexOf(item.typeName)].list.push(item);
                            }
                        })
                    }
                    this.contentList = contentList;
                    this.contentListCopy = JSON.parse(JSON.stringify(contentList))
                    if (res.data.length < this.pageSize) {
                        this.noMore = true;
                    }
                } else {
                    uni.showToast({
                        title: res.returnMessage,
                        icon: "none"
                    });
                }
            })
        },
        // 显示分类选择开关
        classyToogle: function() {
            this.classyShow = !this.classyShow;
        },
        // 分类选择
        classySelect: function(index) {
            this.classyIndex = index;
            this.classyShow = !this.classyShow;
        },
        countCha(type, index, val) {
            let value = val.detail.value
            let count = ''
            if (type === 1) {
                count = this.contentListCopy[this.classyIndex].list[index].newCount
            } else {
                count = this.contentListCopy[this.classyIndex].list[index].residueWeight
            }
            this.contentList[this.classyIndex].list[index].identifyResult = value - count
            console.log(type, index, val);
            console.log(value, count, this.contentList[this.classyIndex].list[index].identifyResult);
        },
        // 上货数量减少
        countSub: function(index) {
            var produce = this.contentList[this.classyIndex].list[index];
            // produce.count--; // 负数未减库存下货
            // produce.residueNum--; // 负数未减库存下货
            var produce2 = this.contentList[this.classyIndex].list[index]
            produce2.identifyResult--
            let saleMode = this.contentList[this.classyIndex].list[index].saleMode
            if (this.deviceType !== 13) {
                this.contentList[this.classyIndex].list[index].residueNum--
            } else if (this.deviceType === 13 && saleMode === 3) {
                this.contentList[this.classyIndex].list[index].newCount--
            } else if (this.deviceType === 13 && saleMode === 2) {
                this.contentList[this.classyIndex].list[index].residueWeight--
            }
        },
        // 上货数量增加
        countAdd: function(index) {
            var produce = this.contentList[this.classyIndex].list[index];
            // produce.count++;
            // produce.residueNum++;
            var produce2 = this.contentList[this.classyIndex].list[index]
            produce2.identifyResult++
            let saleMode = this.contentList[this.classyIndex].list[index].saleMode
            if (this.deviceType !== 13) {
                this.contentList[this.classyIndex].list[index].residueNum++
            } else if (this.deviceType === 13 && saleMode === 3) {
                this.contentList[this.classyIndex].list[index].newCount++
            } else if (this.deviceType === 13 && saleMode === 2) {
                this.contentList[this.classyIndex].list[index].residueWeight++
            }
        },
        // 提交
        post: function() {
            const userId = this.$store.state.userInfo.userId.toString()
            let data = {
                userId: userId,
                recordId: this.$store.state.replenishNumner,
            }
            data.detail = [];
            this.contentList.forEach((val, index) => {
                val.list.forEach((item, index) => {
                    console.log("232", item);
                    if (this.deviceType !== 13) {
                        // 视觉柜
                        if (item.residueNum - item.count != 0) {
                            console.log(11, item);
                            data.detail.push({
                                goodsId: item.goodsId,
                                name: item.goodsName,
                                count: item.residueNum - item.count, //上货数量
                                residueNum: item.residueNum, //剩余库存数量
                            })
                        }
                    } else if (this.deviceType === 13 && item.saleMode == 3) {
                        // 重力柜 
                        console.log("这里呢 ", this.contentList[this.classyIndex].list[index]
                            .identifyResult);
                        if (this.contentList[this.classyIndex].list[index].identifyResult != 0) {
                            data.detail.push({
                                goodsId: item.goodsId,
                                name: item.goodsName,
                                count: item.identifyResult, //上货数量
                                residueNum: item.newCount //剩余库存数量
                            })
                        }
                    } else if (this.deviceType === 13 && item.saleMode == 2) {
                        console.log("有没有走到这一步 的");
                        if (item.identifyResult != 0) {
                            data.detail.push({
                                goodsId: item.goodsId,
                                name: item.goodsName,
                                count: item.identifyResult, //上货数量
                                residueNum: item.residueWeight //剩余库存数量
                            })
                            console.log(264, item);
                        }
                    }
                })
            })
            this.$request(this.$api.PushReplenishSheet, data, "POST", true, "application/json").then((res) => {
                if (res.returnCode == "SUCCESS") {
                    this.$store.state.replenishNumner = ''; // 提交成功后清除全局上货单号
                    uni.showModal({
                        title: '提交成功',
                        content: res.returnMessage,
                        showCancel: false,
                        success() {
                            uni.navigateBack();
                        }
                    });
                } else {
                    uni.showModal({
                        title: '提交失败',
                        content: res.returnMessage,
                        showCancel: false,
                    });
                }
            })
        },
    }
}